package com.x.reader.service;

import com.x.framework.Base;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import com.x.reader.dao.BookDao;
import com.x.reader.model.Book;
import com.x.reader.model.BookNew;
import com.x.reader.model.BookRank;
import com.x.reader.model.BookRecommend;
import com.x.reader.util.PdfUtil;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.directwebremoting.io.FileTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

@Component
public class BookService extends BaseService {

    @Autowired
    private BookDao bookDao;

    public Book insertBook(Book book, FileTransfer bookCoverFile, FileTransfer bookFileFile) {
        try {
            String bookId = this.getUid(32);
            String bookCover = bookCoverFile.getFilename();
            bookCover = bookId + bookCover.substring(bookCover.lastIndexOf(".")).toLowerCase();
            String bookFile = bookFileFile.getFilename();
            bookFile = bookId + bookFile.substring(bookFile.lastIndexOf(".")).toLowerCase();
            book.setBookId(bookId);
            book.setBookCover(bookCover);
            book.setBookFile(bookFile);
            book.setBookCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            book = this.insert(book);
            this.uploadFile(bookCoverFile, "/upload/book/cover", bookCover);
            this.uploadFile(bookFileFile, "/upload/book/file", bookFile);
            return book;
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void updateBook(Book book, FileTransfer bookCoverFile, FileTransfer bookFileFile) {
        try {
            String bookId = book.getBookId();
            String bookCover = null;
            if (Base.isNotNull(bookCoverFile)) {
                bookCover = bookCoverFile.getFilename();
                bookCover = bookId + bookCover.substring(bookCover.lastIndexOf(".")).toLowerCase();
                book.setBookCover(bookCover);
            }
            String bookFile = null;
            if (Base.isNotNull(bookFileFile)) {
                bookFile = bookFileFile.getFilename();
                bookFile = bookId + bookFile.substring(bookFile.lastIndexOf(".")).toLowerCase();
                book.setBookFile(bookFile);
            }
            book.setBookCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            this.update(book);

            if (Base.isNotNull(bookCover)) {
                this.deleteFile("/upload/book/cover", bookCover);
                this.uploadFile(bookCoverFile, "/upload/book/cover", bookCover);
            }
            if (Base.isNotNull(bookFile)) {
                this.deleteFile("/upload/book/file", bookFile);
                this.uploadFile(bookFileFile, "/upload/book/file", bookFile);
            }
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void deleteBook(Book book) {
        try {
            if (Base.isNotNull(book.getBookCover())) {
                this.deleteFile("/upload/book/cover", book.getBookCover());
            }
            if (Base.isNotNull(book.getBookFile())) {
                this.deleteFile("/upload/book/file", book.getBookFile());
            }
            Book b = new Book();
            b.setBookId(book.getBookId());
            this.delete(b);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Book> selectBookList(Book book) {
        try {
            return bookDao.selectBookList(book);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public int selectBookNum(Book book) {
        try {
            return bookDao.selectBookNum(book);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public BookRecommend insertBookRecommend(BookRecommend bookRecommend) {
        try {
            this.delete(bookRecommend);
            bookRecommend.setBookCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            return this.insert(bookRecommend);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void deleteBookRecommend(BookRecommend bookRecommend) {
        try {
            this.delete(bookRecommend);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Book> selectBookRecommendList(Book book) {
        try {
            return bookDao.selectBookRecommendList(book);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public BookNew insertBookNew(BookNew bookNew) {
        try {
            this.delete(bookNew);
            bookNew.setBookCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            return this.insert(bookNew);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void deleteBookNew(BookNew bookNew) {
        try {
            this.delete(bookNew);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Book> selectBookNewList(Book book) {
        try {
            return bookDao.selectBookNewList(book);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public BookRank insertBookRank(BookRank bookRank) {
        try {
            this.delete(bookRank);
            bookRank.setBookCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            return this.insert(bookRank);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void deleteBookRank(BookRank bookRank) {
        try {
            this.delete(bookRank);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Book> selectBookRankList(Book book) {
        try {
            return bookDao.selectBookRankList(book);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public int pdfToImage(String bookId, String bookFile) {
        try {
//            String bookId = book.getBookId();
            int bookPageSize = 0;
            String bookPath = this.getRealPath("/upload/book");
            File bookPdfFile = new File(bookPath + "/file", bookFile);
            if (bookPdfFile.exists() && bookPdfFile.isFile()) {
                PDDocument pdDocument = PDDocument.load(bookPdfFile);
                bookPageSize = pdDocument.getNumberOfPages();
                PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
                if (bookPageSize > 0) {
                    File bookImageDir = new File(bookPath + "/image/" + bookId);
                    if (!bookImageDir.exists()) {
                        bookImageDir.mkdir();
                    }
                    BufferedImage bufferedImage = null;
                    File bookImageFile;
                    String bookImageName;
                    float dpi = 105;
                    String line = "_";
                    String dot = ".";
                    String png = "png";
                    for (int pageIndex = 0; pageIndex < bookPageSize; pageIndex++) {
                        bookImageName = bookId + line + pageIndex + dot + png;
                        bookImageFile = new File(bookImageDir, bookImageName);
                        if (!bookImageFile.exists()) {
                            bufferedImage = pdfRenderer.renderImageWithDPI(pageIndex, dpi, ImageType.RGB);
                            ImageIO.write(bufferedImage, png, bookImageFile);
                        }
                    }
                }
                if (pdDocument != null) {
                    pdDocument.close();
                }
            }
            Book book = new Book();
            book.setBookId(bookId);
            book.setPageSize(bookPageSize);
            this.update(book);
            return bookPageSize;
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }


    public void pdf2txt() {
        try {
//            PdfUtil.readPdf();
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

}
